package com.java28.rbac.controller;

import com.alibaba.fastjson.JSON;
import com.java28.rbac.pojo.Permission;
import com.java28.rbac.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    PermissionService permissionService;

    @RequestMapping("index")
    public String permissionView(){
        return "WEB-INF/permission/permission";
    }


    @RequestMapping("list")
    @ResponseBody
    public List<Permission> list(Integer permissionid){
//        获得集合
        List<Permission> permissionList = permissionService.findAll(permissionid);
//      输出至页面
        return permissionList;
    }


    @RequestMapping("save")
    @ResponseBody
    public String save(Permission permission){
        try{
            permissionService.addPermission(permission);
        }catch(Exception e){
            return "false";
        }
        return "true";
    }

    @RequestMapping("updateView")
    public String update(Integer permissionid, Model model){
        Permission permission = permissionService.findOne(permissionid);
        model.addAttribute("permission",permission);
        return "WEB-INF/permission/updatePermission";
    }


    @RequestMapping("delete")
    @ResponseBody
    public String delete(Integer permissionid){
        try{
            permissionService.delPermission(permissionid);
        }catch(Exception e){
            return "false";
        }
        return "true";
    }


}
